//
//  String+Validated.swift
//  MobileRTCSwiftSample
//
//  Created by zhangheng on 2020/2/9.
//  Copyright © 2020 Zoom Video Communications, Inc. All rights reserved.
//

import Foundation

extension String {
    //1、手机
    static func validatedPhone(phoneStr:String) -> Bool {
        let phone = phoneStr.trimmingCharacters(in: CharacterSet.whitespaces)
        let regex =  "^((13[0-9])|(14[5,7])|(15[^4,\\D]) |(17[0,0-9])|(18[0,0-9]))\\d{8}$"
        let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
        let result = predicate.evaluate(with: phone)
        return result
    }

    //2、邮箱验证
    static func validatedEmail(emailStr:String) -> Bool {
        let email = emailStr.trimmingCharacters(in: CharacterSet.whitespaces)
        let regex =  "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$"
        let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
        let result = predicate.evaluate(with: email)
        return result
    }
    //3、身份证号验证
    static func validatedIdentityCard(identityCardStr:String) -> Bool {
        let identityCard = identityCardStr.trimmingCharacters(in: CharacterSet.whitespaces)
        let regex =  "^(\\d{14}|\\d{17})(\\d|[xX])$"
        let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
        let result = predicate.evaluate(with: identityCard)
        return result
    }
    
    //4、用户昵称验证
    //用户昵称验证（允许使用4-8个汉字）
    static func validatedNickName(nickNameStr:String) -> Bool {
        let nickName = nickNameStr.trimmingCharacters(in: CharacterSet.whitespaces)
    let regex = "[a-zA-Z0-9\\u4e00-\\u9fa5]{1,11}"//"[a-zA-Z\\u4e00-\\u9fa5][a-zA-Z0-9\\u4e00-\\u9fa5]+"//"^[\\u4e00-\\u9fa5]{1,11}$"//"^[a-zA-Z0-9_-\\u4e00-\\u9fa5]{1,11}$"
        let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
        let result = predicate.evaluate(with: nickName)
        return result
    }
    
    //5、用户名验证
    //用户名验证（允许使用小写字母、数字、下滑线、横杠，一共3~16个字符）
    static func validatedUserName(userNameStr:String) -> Bool {
        let userName = userNameStr.trimmingCharacters(in: CharacterSet.whitespaces)
        let regex =  "^[a-z0-9_-]{3,16}$"
        let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
        let result = predicate.evaluate(with: userName)
        return result
    }
    
    //6、密码强度验证
    //密码的强度必须是包含大小写字母和数字的组合，不能使用特殊字符，长度在8-10之间。
    static func validatedPassword(passwordStr:String) -> Bool {
        let password = passwordStr.trimmingCharacters(in: CharacterSet.whitespaces)
        let regex =  "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$"
        let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
        let result = predicate.evaluate(with: password)
        return result
    }
    
    //7、URL验证
    static func validatedURL(urlStr:String) -> Bool {
        let url = urlStr.trimmingCharacters(in: CharacterSet.whitespaces)
        let regex =  "^(https?:\\\\)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\w \\.-]*)*\\?$"
        let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
        let result = predicate.evaluate(with: url)
        return result
    }
    
    //8、IP-v4地址验证
    static func validatedIPv4(ipv4Str:String) -> Bool {
        let ipv4 = ipv4Str.trimmingCharacters(in: CharacterSet.whitespaces)
        let regex =  "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
        let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
        let result = predicate.evaluate(with: ipv4)
        return result
    }
    
    //9、IP-v6地址验证
    static func validatedIPv6(ipv6Str:String) -> Bool {
        let ipv6 = ipv6Str.trimmingCharacters(in: CharacterSet.whitespaces)
            let regex =  "(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))"
        let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
        let result = predicate.evaluate(with: ipv6)
        return result
    }
    
    //10、HTML标签验证
    static func validatedHtml(htmlStr:String) -> Bool {
        let html = htmlStr.trimmingCharacters(in: CharacterSet.whitespaces)
        let regex =  "<\\/?\\w+((\\s+\\w+(\\s*=\\s*(?:\".*?\"|'.*?'|[\\^'\">\\s]+))?)+\\s*|\\s*)\\/?>"
        let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
        let result = predicate.evaluate(with: html)
        return result
    }
    
    //11、车牌号验证
    static func validatedCarNo(carNoStr:String) -> Bool {
        let carNo = carNoStr.trimmingCharacters(in: CharacterSet.whitespaces)
        let regex =  "^[\\u4e00-\\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\\u4e00-\\u9fa5]$"
        let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
        let result = predicate.evaluate(with: carNo)
        return result
    }
    
    //12、车型验证
    static func validatedCarType(carTypeStr:String) -> Bool {
        let carType = carTypeStr.trimmingCharacters(in: CharacterSet.whitespaces)
        let regex =  "^[\\u4e00-\\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\\u4e00-\\u9fa5]$"
        let predicate = NSPredicate(format:"SELF MATCHES %@",regex)
        let result = predicate.evaluate(with: carType)
        return result
    }

}
